package com.zrrd.homework._0325_0331.h6;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title Mall
 * @description
 * @create 2024/3/31
 */
public class Mall {

    List<Product> products;
    //初始化代码块
    {
        products=new ArrayList<>();
        products.add(new Product("花生",10));
        products.add(new Product("瓜子",10));
        products.add(new Product("矿泉水",10));
        products.add(new Product("啤酒",10));
        products.add(new Product("饮料",10));
        products.add(new Product("八宝粥",10));
    }

    /**
     *
     * @param productName
     * @param num
     * @return
     */
    public Product buy(String productName,int num){
        for (int i = 0; i < products.size(); i++) {
            Product product = products.get(i);
            String pName = product.getName();
            int pCount = product.getCount();
            if (pName.equals(productName)){
                if (pCount!=0&&pCount>=num){
                    product.setCount(pCount-num);
                    //返回购买的商品信息
                    return product;
                }
            }
        }
        throw new ProductException();
    }
}
